package com.ruoyi.web.controller.iot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.iot.WatchData;
import com.ruoyi.system.service.IotLocationService;
import com.ruoyi.web.controller.iot.bean.Device;
import com.ruoyi.web.controller.iot.service.DuDeomoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/du")
public class DUIotDemoController extends BaseController {

    private final static String gkdeKey = "cd701bb48aee4d974508334066d33c5c";

    @Autowired
    private DuDeomoService duDeomoService;

    @Autowired
    private IotLocationService locationService;

    private static final String trid = "140";
    /**
     * UD_LTE,
     * 091223,
     * 124319,
     * A,
     * 32.854426,N,115.8473137,E,
     * 43.65 速度
     * ,47.5, 方向
     * 0.0, 海拔
     * 17, 卫星个数
     * 67 信号强度
     * ,61, 电量
     * 0, 计步数
     * 0, 翻转次数
     * 00000000, 终端状态
     * 1,   0,  460,     0,        22013,99434777   ,149,
     * 基站 延时 国家码MCC 0:中国移动  区域码 基站编号    信号强度
     * 4,,0:5c:c2:29:11:ed,-76,,80:89:17:ec:62:a3,-80,,70:85:c4:4f:f4:e1,-81,,5c:de:34:e8:3c:56,-85,
     * 0.0]
     */
    @GetMapping("/getLocation")
    public List<WatchData> getLocation(){
        String imei = "9618021514"; // 设备编号
        String time = "2023-12-06"; // 轨迹日期 默认一天
        // 查询数据库数据
        List<WatchData> list = locationService.getDeviceList(imei,time);
        // 组装数据返回格式

        list.stream().forEach(wd->{
          Map<String,Object> map = duDeomoService.getWatchLocation(imei,wd.getDeviceLocationData());
          if (null != map){
              wd.setMap(map);
          }
        });

        List<double[]> temp = list.stream().map(wd->{
            Map<String,Object> map = wd.getMap();
            if (null != map){
                List<String> pointLocation = Arrays.asList(map.get("location").toString().split(","));
                double[] pl = {Double.parseDouble(pointLocation.get(0)),Double.parseDouble(pointLocation.get(1))};
                return pl;
            }
            return null;
        }).collect(Collectors.toList());
        System.out.println("====================================================== 转换高德原始位置  start ===========================================================");
        System.out.println(JSONArray.toJSONString(temp)+" temp: "+temp.size()+" list: "+list.size());
        System.out.println("====================================================== 转换高德原始位置  end ===========================================================");
        duDeomoService.addPointLbs(list,trid);

        List<Device> oldPoint =list.stream().map(dd->{
            Device device = new Device();
            List<String> pointLocation = Arrays.asList(dd.getMap().get("location").toString().split(","));
            device.setLatitude((float)Float.valueOf(pointLocation.get(1)));
            device.setLongitude((float)Float.valueOf(pointLocation.get(0)));
            device.setLast_upload_at(Long.valueOf(dd.getMap().get("locatetime").toString()));
            device.setSpeed(Float.valueOf(dd.getMap().get("speed").toString()));
            return device;
        }).collect(Collectors.toList());

        JSONObject result = duDeomoService.getWatchPointLbs(trid,time);
        JSONObject points = result.getJSONObject("data").getJSONArray("tracks").getJSONObject(0);
        List<Device> locationList = points.getJSONArray("points").stream().map(js->{
            JSONObject point = (JSONObject) js;
            Device device = new Device();
            List<String> pointLocation = Arrays.asList(point.getString("location").split(","));
            double[] pl = {Double.parseDouble(pointLocation.get(0)),Double.parseDouble(pointLocation.get(1))};
            device.setLatitude((float)Float.valueOf(pointLocation.get(1)));
            device.setLongitude((float)Float.valueOf(pointLocation.get(0)));
            device.setLast_upload_at(point.getLong("locatetime"));
            return device;
        }).collect(Collectors.toList());
        System.out.println("====================================================== 纠偏位置  start ===========================================================");
        System.out.println(JSONArray.toJSONString(locationList.stream().map(dd->new float[]{dd.getLongitude(),dd.getLatitude()}).collect(Collectors.toList())));
        System.out.println("====================================================== 纠偏位置  end ===========================================================");
        // 原始点位 和纠正后的点位进行 整合保留细节点位
       List<Device> devicesPoint = duDeomoService.correctionOfPointDetails(oldPoint,locationList);
       List<float[]> localPoint = devicesPoint.stream().map(ds->{
           return new float[]{ds.getLongitude(), ds.getLatitude()};
       }).collect(Collectors.toList());
        System.out.println("====================================================== 细节延迟点位处理 start ===========================================================");
        System.out.println(JSONArray.toJSONString(localPoint));
        System.out.println("====================================================== 细节延迟点位处理  end ===========================================================");

        return list;
    }




}
